package com.bdqn.service;

import com.bdqn.mapper.StorageRecordMapper;
import com.bdqn.pojo.StorageRecord;
import com.bdqn.util.MyBatisUtil;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.session.SqlSession;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * 入库功能模块业务实现类
 */
public class StorageRecordServiceImpl implements StorageRecordService {
    private Logger logger = LogManager.getLogger(StorageRecordServiceImpl.class);

    /**
     * 分页查询入库列表
     *
     * @param pageNo
     * @param pageSize
     * @param goodsName
     * @param supplierId
     * @param payStatus
     * @return
     */
    public List<StorageRecord> selectPageList(@Param("pageNo") Integer pageNo, @Param("pageSize") Integer pageSize,
                                              @Param("goodsName") String goodsName, @Param("supplierId") Integer supplierId,
                                              @Param("payStatus") Integer payStatus) {
        if(pageNo == null || pageSize == null){
            return null;
        }
        SqlSession sqlSession = null;
        List<StorageRecord> list = null;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            list = sqlSession.getMapper(StorageRecordMapper.class).selectPageList(pageNo,pageSize,goodsName,supplierId,payStatus);
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return list;
    }

    /**
     * 查询总记录数
     *
     * @param goodsName
     * @param supplierId
     * @param payStatus
     * @return
     */
    @Override
    public int count(String goodsName,Integer supplierId,Integer payStatus){
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(StorageRecordMapper.class).count(goodsName,supplierId,payStatus);
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据ID查询入库信息
     *
     * @param id 查询条件
     * @return
     */
    @Override
    public StorageRecord getStorageRecord(Integer id) {
        if(id == null){
            return null;
        }
        SqlSession sqlSession = null;
        StorageRecord storageRecord = null;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            storageRecord = sqlSession.getMapper(StorageRecordMapper.class).getStorageRecord(id);
        }catch (Exception e){
            logger.error(e.getMessage());
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return storageRecord;
    }

    /**
     * 增加入库信息
     *
     * @param storageRecord
     * @return
     */
    @Override
    public int add(StorageRecord storageRecord) {
        if(storageRecord == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(StorageRecordMapper.class).add(storageRecord);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据ID修改入库信息
     *
     * @param storageRecord
     * @return
     */
    @Override
    public int update(StorageRecord storageRecord) {
        if(storageRecord == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(StorageRecordMapper.class).update(storageRecord);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 根据ID删除入库信息
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Integer id) {
        if(id == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(StorageRecordMapper.class).delete(id);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }

    /**
     * 更改指定商品的库存量
     *
     * @param id
     * @param goodsCount
     * @return
     */
    @Override
    public int updGoodsCount(Integer id, Integer goodsCount) {
        if(id == null || goodsCount == null){
            return 0;
        }
        SqlSession sqlSession = null;
        int count = 0;
        try{
            sqlSession = MyBatisUtil.createSqlSession();
            count = sqlSession.getMapper(StorageRecordMapper.class).updGoodsCount(id,goodsCount);
            sqlSession.commit();
        }catch (Exception e){
            logger.error(e.getMessage());
            sqlSession.rollback();
        } finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        return count;
    }


}
